package Eyjafjalla.dungeons;

import Eyjafjalla.helper.DungeonHelper;
import Eyjafjalla.monster.Dolly;
import com.badlogic.gdx.graphics.Color;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.localization.UIStrings;
import com.megacrit.cardcrawl.map.MapEdge;
import com.megacrit.cardcrawl.map.MapGenerator;
import com.megacrit.cardcrawl.map.MapRoomNode;
import com.megacrit.cardcrawl.monsters.MonsterInfo;
import com.megacrit.cardcrawl.random.Random;
import com.megacrit.cardcrawl.rooms.*;
import com.megacrit.cardcrawl.saveAndContinue.SaveFile;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;

public class TheSiesta extends AbstractDungeon {
    private static final Logger logger = LogManager.getLogger(TheSiesta.class.getName());
    private static final UIStrings uiStrings;
    public static final String[] TEXT;
    public static final String NAME;
    public static final String ID = "eyjafjalla:TheSiesta";

    public TheSiesta(AbstractPlayer p, ArrayList<String> theList){
        super(NAME, ID, p, theList);
        if (scene != null) {
            scene.dispose();
        }

        scene = new TheSiestaScene();
        fadeColor = Color.valueOf("140a1eff");
        sourceFadeColor = Color.valueOf("140a1eff");
        this.initializeLevelSpecificChances();
        mapRng = new Random(Settings.seed + (long)(AbstractDungeon.actNum * 300));
        this.generateSpecialMap();
        CardCrawlGame.music.changeBGM(id);
    }

    public TheSiesta(AbstractPlayer p, SaveFile saveFile){
        super(NAME, p, saveFile);
        CardCrawlGame.dungeon = this;
        if (scene != null) {
            scene.dispose();
        }

        scene = new TheSiestaScene();
        fadeColor = Color.valueOf("140a1eff");
        sourceFadeColor = Color.valueOf("140a1eff");
        this.initializeLevelSpecificChances();
        miscRng = new Random(Settings.seed + (long)saveFile.floor_num);
        CardCrawlGame.music.changeBGM(id);
        mapRng = new Random(Settings.seed + (long)(saveFile.act_num * 300));
        this.generateSpecialMap();
        firstRoomChosen = true;
        this.populatePathTaken(saveFile);
    }

    private void generateSpecialMap(){
        long startTime = System.currentTimeMillis();
        map = new ArrayList();
        MapRoomNode rest30 = new MapRoomNode(3,0);
        rest30.room =  new RestRoom();
        MapRoomNode elite31 = new MapRoomNode(3,1);
        elite31.room = new MonsterRoomElite();
        MapRoomNode monster22 = new MapRoomNode(2,2);
        monster22.room = new MonsterRoom();
        MapRoomNode event42 = new MapRoomNode(4,2);
        event42.room = new EventRoom();
        MapRoomNode rest13 = new MapRoomNode(1,3);
        rest13.room = new RestRoom();
        MapRoomNode chest33 = new MapRoomNode(3,3);
        chest33.room = new TreasureRoom();
        MapRoomNode shop53 = new MapRoomNode(5,3);
        shop53.room = new ShopRoom();
        MapRoomNode event24 = new MapRoomNode(2,4);
        event24.room = new EventRoom();
        MapRoomNode monster44 = new MapRoomNode(4,4);
        monster44.room = new MonsterRoom();
        MapRoomNode elite35 = new MapRoomNode(3,5);
        elite35.room = new MonsterRoomElite();
        MapRoomNode boss36 = new MapRoomNode(3,6);
        boss36.room = new MonsterRoomBoss();
        MapRoomNode victory37 = new MapRoomNode(3,7);
        victory37.room = new TrueVictoryRoom();

        map.add(DungeonHelper.createRow(0,rest30));
        map.add(DungeonHelper.createRow(1,elite31));
        map.add(DungeonHelper.createRow(2,monster22,event42));
        map.add(DungeonHelper.createRow(3,rest13,chest33,shop53));
        map.add(DungeonHelper.createRow(4,event24,monster44));
        map.add(DungeonHelper.createRow(5,elite35));
        map.add(DungeonHelper.createRow(6,boss36));
        map.add(DungeonHelper.createRow(7,victory37));

        DungeonHelper.connectNode(rest30,elite31);
        DungeonHelper.connectNode(elite31,event42);
        DungeonHelper.connectNode(event42,shop53);
        DungeonHelper.connectNode(shop53,monster44);
        DungeonHelper.connectNode(monster44,chest33);
        DungeonHelper.connectNode(chest33,monster22);
        DungeonHelper.connectNode(monster22,rest13);
        DungeonHelper.connectNode(rest13,event24);
        DungeonHelper.connectNode(event24,elite35);
        DungeonHelper.connectNode(elite35,boss36);

        logger.info("Generated the following dungeon map:");
        logger.info(MapGenerator.toString(map, true));
        logger.info("Game Seed: " + Settings.seed);
        logger.info("Map generation time: " + (System.currentTimeMillis() - startTime) + "ms");
        firstRoomChosen = false;
        fadeIn();
    }

    @Override
    protected void initializeLevelSpecificChances() {
        shopRoomChance = 0F;
        restRoomChance = 0F;
        treasureRoomChance = 0.0F;
        eventRoomChance = 1F;
        eliteRoomChance = 0F;
        smallChestChance = 0;
        mediumChestChance = 100;
        largeChestChance = 0;
        commonRelicChance = 0;
        uncommonRelicChance = 0;
        rareRelicChance = 100;
        colorlessRareChance = 0.3F;
        if (AbstractDungeon.ascensionLevel >= 12) {
            cardUpgradedChance = 0.5F;
        } else {
            cardUpgradedChance = 1F;
        }
    }

    @Override
    protected void generateMonsters() {
        monsterList = new ArrayList<>();
        monsterList.add("N_E_TQB");
        monsterList.add("N_E_CPS_CPS");
        monsterList.add("H_E_TQB_LTTQB");
        monsterList.add("H_E_CPS_LTTQB");
        Collections.shuffle(monsterList,monsterRng.random);

        eliteMonsterList = new ArrayList<>();
        eliteMonsterList.add("E_E_DGDGZ");
        eliteMonsterList.add("E_E_JMCPS_MTFKY");
        eliteMonsterList.add("E_E_CXXSS_4");
        Collections.shuffle(eliteMonsterList,monsterRng.random);
    }

    @Override
    protected void generateWeakEnemies(int i) {
        ArrayList<MonsterInfo> monsters = new ArrayList();
        monsters.add(new MonsterInfo("N_E_TQB",1F));
        monsters.add(new MonsterInfo("N_E_CPS_CPS",1F));
        MonsterInfo.normalizeWeights(monsters);
        this.populateMonsterList(monsters, i, false);
    }

    @Override
    protected void generateStrongEnemies(int i) {
        ArrayList<MonsterInfo> monsters = new ArrayList<>();
        monsters.add(new MonsterInfo("H_E_TQB_LTTQB",1F));
        monsters.add(new MonsterInfo("H_E_CPS_LTTQB",1F));
        monsters.add(new MonsterInfo("H_E_TQB_LTTQB",1F));
        monsters.add(new MonsterInfo("H_E_CPS_LTTQB",1F));
        //autoAdd
        MonsterInfo.normalizeWeights(monsters);
        this.populateFirstStrongEnemy(monsters, this.generateExclusions());
        this.populateMonsterList(monsters, i, false);
    }

    @Override
    protected void generateElites(int i) {
        ArrayList<MonsterInfo> elitemonsters = new ArrayList<>();
        elitemonsters.add(new MonsterInfo("Shield and Spear",3F));
        elitemonsters.add(new MonsterInfo("E_E_JMCPS_MTFKY",3F));
        elitemonsters.add(new MonsterInfo("E_E_JMCPS_MTFKY",3F));
        //autoAdd
        MonsterInfo.normalizeWeights(elitemonsters);
        this.populateMonsterList(elitemonsters, i, true);
    }

    @Override
    protected ArrayList<String> generateExclusions() {
        return new ArrayList<>();
    }

    @Override
    protected void initializeBoss() {
        bossList = new ArrayList<>();
        bossList.add(Dolly.ID);
        bossList.add(Dolly.ID);
        bossList.add(Dolly.ID);
    }

    @Override
    protected void initializeEventList() {
        //eventList = new ArrayList<>();
        //todo add
    }

    @Override
    protected void initializeEventImg() {
        if (eventBackgroundImg != null) {
            eventBackgroundImg.dispose();
            eventBackgroundImg = null;
        }

        eventBackgroundImg = ImageMaster.loadImage("images/ui/event/panel.png");
    }

    @Override
    protected void initializeShrineList() {

    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(ID);
        TEXT = uiStrings.TEXT;
        NAME = TEXT[0];
    }
}

